#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include "ddebug.h"
#include "global_data.h"
#include "message_recv.h"
#include "shmctl.h"
#include "zigbee.h"
#include "gprs.h"
#define ZIGBEE_DEV "/dev/ttyUSB0"
#define GPRS_DEV "/dev/ttyUSB1"
int sto_auto_status = 0;
void *auto_sto(void *argc);
int main(int argc, char *argv[])
{
//	char buf[20] = {0};
	int i;
	int fd;
	int pthread_kill_err;
	struct shm_addr *shm_buf = NULL;//(struct shm_addr *)malloc(sizeof(struct shm_addr));
	init_sem(0,1);
//	memset(shm_buf, 0 ,sizeof(struct shm_addr));
	/*int fd_zigbee = */
	serial_init_a9(ZIGBEE_DEV);
	serial_init(GPRS_DEV);
	set_phone_number((unsigned char *)"\"18997215973\"");
/*	shmget_env(shm_buf);
	shm_buf->rt_status.storage_no[1].storage_status = 0;
	shm_buf->rt_status.storage_no[2].storage_status = 1;
	shm_buf->rt_status.storage_no[1].led_status = 1;
	shm_buf->rt_status.storage_no[1].buzzer_status = 1;
	shm_buf->rt_status.storage_no[1].fan_status = 2;
	shm_buf->rt_status.storage_no[1].seg_status = 8;
	shm_buf->rt_status.storage_no[1].temperature = 20.9;

	shm_buf->rt_status.storage_no[1].humidity = 11.1;
	shm_buf->rt_status.storage_no[1].illumination = 1.1;
	shm_buf->rt_status.storage_no[1].battery = 3.7;
	shm_buf->rt_status.storage_no[1].adc = 2;
	shm_buf->rt_status.storage_no[1].x = 3;
	shm_buf->rt_status.storage_no[1].y = 4;
	shm_buf->rt_status.storage_no[1].z = 5;

	shm_buf->rt_status.storage_no[1].storage_status = 1;
	
	shmset_env(shm_buf);
*/
	pthread_t tid_msg,tid_zgb;
/*	pthread_attr_t attr;
	pthread_attr_init(&attr);
 	pthread_attr_setstacksize(&attr, 10*1024*1024);
*/

	pthread_create(&tid_msg, NULL, message_recv, NULL);
	pthread_create(&tid_zgb, NULL, zigbee_recv, NULL);
//	pthread_create(&tid_ato, NULL, auto_sto, NULL);
	//zigbee_recv(NULL);
//	message_recv(NULL);
	
	fd = open("/root/deamonfifo",O_WRONLY);
	while(1)
	{


		sleep(2);
		pid_t tt = getpid();
		fflush(stdout);
		write(fd,&tt,sizeof(pid_t));

		pthread_kill_err = pthread_kill(tid_msg,0);
		if(pthread_kill_err == ESRCH)
		{
			DDEBUG(P_URGENCY,"<<Main>> ID 0x%x,name is Messagerecv is quit\n",(unsigned int)tid_msg);
			break;
		}
		else if(pthread_kill_err == EINVAL)
		{
			DDEBUG(P_URGENCY,"<<Main>> illegal signal\n");
			break;
		}
		else
			DDEBUG(P_NORMAL,"<<Main>> ID 0x%x,name is Messagerecv is alive\n",(unsigned int)tid_msg);

		pthread_kill_err = pthread_kill(tid_zgb,0);
		if(pthread_kill_err == ESRCH)
		{
			DDEBUG(P_URGENCY,"<<Main>> ID 0x%x,name is ZigbeeRecv is quit\n",(unsigned int)tid_zgb);
			break;
		}
		else if(pthread_kill_err == EINVAL)
		{	
			DDEBUG(P_URGENCY,"<<Main>> illegal signal\n");
			break;
		}
		else
			DDEBUG(P_NORMAL,"<<Main>> ID 0x%x,name is ZigbeeRecv is alive\n",(unsigned int)tid_zgb);

		sto_auto_status += 0x11111111;
		DDEBUG(P_NORMAL,"<<Main>> auto--------------------------<<<<<<<<<%08x\n",sto_auto_status);
		for(i = 0; i < 8; i++)
		{
			if(sto_auto_status & (0x8 << (i * 4)))
			{
				shmget_env(&shm_buf);
				shm_buf->rt_status.storage_no[i].storage_status = 0;
				shmset_env(shm_buf);
				sto_auto_status &= ~(0x8 << (i * 4));
				DDEBUG(P_NORMAL,"<<Main>> Storage %d is closed!\n", i);
			}
		}


	//	printf("Input something:\n");
	//	fgets(buf, 10, stdin);
	}
	return 0;
}
/*
 *自动刷新仓库状态，每个仓库用4个位的数，这个数每两秒加一，这个数字会在收到zigbee消息后被清零
 *参见zigbee_recv函数，sto_auto_status这个变量在zigbee_recv.c文件中是外部变量。
 *如果发现到2*8=16s之后，
 *这个数字没有被清理，则认为这个仓库已经关闭了，将其仓库状态设置为关闭。
 *这个线程放到主线程里面执行 
void *auto_sto(void *argc)
{
	while(1)
	{
	}
}
***************************************/
